package util
{
	import com.hurlant.crypto.Crypto;
	import com.hurlant.crypto.hash.IHash;
	import com.hurlant.crypto.prng.Random;
	import com.hurlant.util.Hex;
	
	import flash.utils.ByteArray;
	
	public class MD5_Sess
	{
		public function MD5_Sess()
		{
		}
		
        
        public static function assembelResponse(asswd : String,registerId:String):String{
        	var returnVal:String = "";
        	/* var key: String = md5.getMD5(asswd);
        	var h1 :String = key + ":nonce:conoce" ;
        	    h1 = md5.getMD5(h1);
        	var h2 :String = md5.getMD5(registerId);
        	returnVal = h1 + ":nonce:" + h2 ;
        	returnVal = md5.getMD5(returnVal); */
        	return  returnVal ;
        }
        
        public static function  GenNonce():String
		{
			var random:Random = new Random();
			var length:int = 16;
			var  buffer:ByteArray = new ByteArray();
			for (var i:int = 0; i < length; i++)
			{
				buffer.writeByte(random.nextByte());
			}
			return  Hex.fromArray(buffer).toUpperCase();
		}
        
        public static function result( sid:String,  password:String,  domain:String,
			 nonce:String,  cnonce:String):String
		{
			    var rst:String = null;
				var buff1:ByteArray = ComputeKey(sid, domain, password);
				var text1:String = ComputeH1(buff1, nonce, cnonce);
				var text2:String = ComputeH2(sid);
				rst = result2(text1, text2, nonce);
			return rst;
	
		}

		public static function  result2( h1:String,  h2:String,  nonce:String):String
		{
			var text1:String = h1 + ":" + nonce + ":" + h2;
			return MD5ToHex(text1).toUpperCase();
		}
	
		public static function ComputeH1( key:ByteArray,  nonce:String,  cnonce:String):String
		{
		
		    var text1:String = ":" + nonce + ":" + cnonce;
		    //var de:String ;
		    //de = ByteArryaToString(key);
		    
			var buffer1:ByteArray = new ByteArray();
			buffer1.writeUTFBytes(text1);
			//de = ByteArryaToString(buffer1);
			
			var buffer2:ByteArray = new ByteArray();
			buffer2.writeBytes(key);
			buffer2.writeBytes(buffer1);
			//de = ByteArryaToString(buffer2);
			
			var hash:IHash = Crypto.getHash("md5");
			var data:ByteArray;
			data = hash.hash(buffer2);
			return Hex.fromArray(data).toUpperCase();
			 
		}

		public static function  ComputeH2( sid:String):String
		{
			var text1:String = "REGISTER:" + sid;
			return MD5ToHex(text1).toUpperCase();
		}
	
		public static function ComputeKey( sid:String,  domain:String,  password:String):ByteArray
		{
			var text:String = sid + ":" + domain + ":" + password;
			
			return MD5(text);
		}
	
		public static function MD5(str:String) :ByteArray
		{
			var currentResult:ByteArray;
			var hash:IHash = Crypto.getHash("md5");
			var data:ByteArray;
			data = Hex.toArray(Hex.fromString(str));
			currentResult = hash.hash(data);
			return currentResult;
		}

		public static function MD5ToHex(str:String):String
		{
			var currentResult:ByteArray = MD5(str);
			var a:String= Hex.fromArray(currentResult);
			return a ;
		}
		
		public static function MD5ToString(str:String):String{
			var currentResult:ByteArray = MD5(str);
			var a:String ="" ;
			currentResult.position = 0 ;
			while(currentResult.position < (currentResult.length-1)){
				a =a + "," +currentResult.readByte();
			}
		
			return a ;
		}
	    
	    public static function ByteArryaToString(str:ByteArray):String{
			var a:String  ;
			str.position = 0 ;
			while(str.position < str.length){
				a =a + "," +str.readByte();
			}	
			return a ;
		}
        
	}
}